﻿using MediatR;
using SystemHYS.API.Application.command.GualitySchemeCommmand;
using SystemHYS.Domian;
using SystemHYS.Domian.DTO;
using SystemHYS.Infrstructre.Interface;
namespace SystemHYS.API.Application.commandler.GualitySchemeCommandler
{
    public class GualitySchemeShowCommmandler:IRequestHandler<GualitySchemeShowCommmand,PageDTO<GualityScheme>>
    {
        IGualitySchemeRepository Repository;
        public GualitySchemeShowCommmandler(IGualitySchemeRepository repository)
        {
            this.Repository = repository;
        }

        public async Task<PageDTO<GualityScheme>> Handle(GualitySchemeShowCommmand request, CancellationToken cancellationToken)
        {
            var list=from a in await Repository.GetAllAsnc() where a.IsDel==false select a;
            if(request.gualitySchemeNumbering!=null )
            {
                list = list.Where(x => x.gualitySchemeNumbering.Contains(request.gualitySchemeNumbering));
            }
            if (request.state != null)
            {
                list=list.Where(x=>x.state==request.state);
            }
            PageDTO<GualityScheme> page = new PageDTO<GualityScheme>();
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(list.Count()*1.0 / request.pagesize);
            page.plist=list.OrderByDescending(z=>z.AddTime).Skip((request.pageindex-1)*request.pagesize).Take(request.pagesize).ToList();
            return page;
        }
    }
}
